package com.yft.data.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	
	
	public static Integer dealNull(Integer type) {
		if(type==null)
		{
			return 0;
		}
		return type;
	}

	public static String dealNull(String str) {
		if(str==null)
		{
			return "";
		}
		return str;
	}

	public static Integer strToInt(String str) {
		if(str.equals("") || str==null)
		{
			return 0;
		}else
		{
			return Integer.parseInt(str);
		}
	}
	/**
	 * 判断html中是否含有图片的链接，有的话，返回图片地址
	 * @param html
	 * @return
	 */
	public static String isIncludeImg(String html)
	{
		Pattern pattern = Pattern.compile("(?<=src\\=\\\")[^\\\"]+(?=\\\")");
		Matcher matcher = pattern.matcher(html);
		if (matcher.find()) {
			return matcher.group().replace("&amp;","&");
		}else
		{
			return "";
		}
	}
	
	/**
	 * 去除所有html标签
	 * @param html
	 * @return
	 */
	public static String filterAllTag(String html) {
		return html.replaceAll("<[^>]*>", "").trim();
	}
	/**
	   * 得到几天前的时间
	   * @param d
	   * @param day
	   * @return
	   */
	  public static Date getDateBefore(Date d,int day){
	   Calendar now =Calendar.getInstance();
	   now.setTime(d);
	   now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
	   return now.getTime();
	  }
	  
	  /**
	   * 得到几天后的时间
	   * @param d
	   * @param day
	   * @return
	   */
	  public static Date getDateAfter(Date d,int day){
	   Calendar now =Calendar.getInstance();
	   now.setTime(d);
	   now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
	   return now.getTime();
	  }
	/**
	 * 日期比较
	 * @param oldtime
	 * @return
	 */
	public static String getCompareTime(String oldtime) {
		// 返回的字符串
		String retStr = "";
		// 传入的日期是2008/9/12等，不作处理
		if (oldtime.indexOf("/") > 0) {
			retStr = oldtime;
			//System.out.println(retStr);
			return retStr;
		} else {
			try {
//				DateFormat hour = new SimpleDateFormat("HH");
//				DateFormat minute = new SimpleDateFormat("mm");
				Date today = new Date();
//				// 得到当前时间的日，小时，分钟
//				int nowHour = Integer.parseInt(hour.format(today));
//				int nowMinute = Integer.parseInt(minute.format(today));
//				System.out.println(nowHour);
				// 格式化传入的字符串
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dataTime = df.parse(oldtime);
				// 得到传入时间的日，小时，分钟
//				int dataHour = Integer.parseInt(hour.format(dataTime));
//				int dataMinute = Integer.parseInt(minute.format(dataTime));
//				System.out.println(dataHour);
				int days = StringUtil.daysOfTwo(dataTime,today);
				
				if (days>=1&&days < 30 ) {
					retStr = days+"天前";
				} else if (days > 30 && days<365) {
					int mounth = (int)days/30;
					int days2 = days-mounth*30;
					retStr = mounth+"月"+days2+"天前";
				} else if (days>365) {
					retStr = "一年前";
				}
				else if (days < 1) {
					int minutes = (int) ((today.getTime() - dataTime.getTime())/(1000*60));
					if (minutes >= 60) {
						retStr = minutes/60 + "小时前";
					} else {
						retStr = minutes + "分钟前";
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return retStr;
	}
	public static int daysOfTwo(Date fDate, Date oDate) {
	       Calendar aCalendar = Calendar.getInstance();
	       aCalendar.setTime(fDate);
	       int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
	       aCalendar.setTime(oDate);
	       int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
	       return (day2-day1);
	}
	
	
}
